package oa.database.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import oa.database.bean.Department;

import oa.database.bean.Employee;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table EMPLOYEE.
*/
public class EmployeeDao extends AbstractDao<Employee, Long> {

    public static final String TABLENAME = "EMPLOYEE";

    /**
     * Properties of entity Employee.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Uid = new Property(0, Long.class, "uid", true, "UID");
        public final static Property Vid = new Property(1, Long.class, "vid", false, "VID");
        public final static Property UserName = new Property(2, String.class, "userName", false, "USER_NAME");
        public final static Property HearImage = new Property(3, String.class, "hearImage", false, "HEAR_IMAGE");
        public final static Property Online = new Property(4, Boolean.class, "online", false, "ONLINE");
        public final static Property GangWei = new Property(5, String.class, "gangWei", false, "GANG_WEI");
        public final static Property IsUsual = new Property(6, Boolean.class, "isUsual", false, "IS_USUAL");
        public final static Property Pid = new Property(7, Long.class, "pid", false, "PID");
    };

    private DaoSession daoSession;

    private Query<Employee> department_EmployeeListQuery;

    public EmployeeDao(DaoConfig config) {
        super(config);
    }
    
    public EmployeeDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'EMPLOYEE' (" + //
                "'UID' INTEGER PRIMARY KEY ," + // 0: uid
                "'VID' INTEGER," + // 1: vid
                "'USER_NAME' TEXT NOT NULL ," + // 2: userName
                "'HEAR_IMAGE' TEXT," + // 3: hearImage
                "'ONLINE' INTEGER," + // 4: online
                "'GANG_WEI' TEXT," + // 5: gangWei
                "'IS_USUAL' INTEGER," + // 6: isUsual
                "'PID' INTEGER);"); // 7: pid
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'EMPLOYEE'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Employee entity) {
        stmt.clearBindings();
 
        Long uid = entity.getUid();
        if (uid != null) {
            stmt.bindLong(1, uid);
        }
 
        Long vid = entity.getVid();
        if (vid != null) {
            stmt.bindLong(2, vid);
        }
        stmt.bindString(3, entity.getUserName());
 
        String hearImage = entity.getHearImage();
        if (hearImage != null) {
            stmt.bindString(4, hearImage);
        }
 
        Boolean online = entity.getOnline();
        if (online != null) {
            stmt.bindLong(5, online ? 1l: 0l);
        }
 
        String gangWei = entity.getGangWei();
        if (gangWei != null) {
            stmt.bindString(6, gangWei);
        }
 
        Boolean isUsual = entity.getIsUsual();
        if (isUsual != null) {
            stmt.bindLong(7, isUsual ? 1l: 0l);
        }
 
        Long pid = entity.getPid();
        if (pid != null) {
            stmt.bindLong(8, pid);
        }
    }

    @Override
    protected void attachEntity(Employee entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Employee readEntity(Cursor cursor, int offset) {
        Employee entity = new Employee( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // uid
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // vid
            cursor.getString(offset + 2), // userName
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // hearImage
            cursor.isNull(offset + 4) ? null : cursor.getShort(offset + 4) != 0, // online
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // gangWei
            cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0, // isUsual
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7) // pid
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Employee entity, int offset) {
        entity.setUid(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setVid(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setUserName(cursor.getString(offset + 2));
        entity.setHearImage(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setOnline(cursor.isNull(offset + 4) ? null : cursor.getShort(offset + 4) != 0);
        entity.setGangWei(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setIsUsual(cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0);
        entity.setPid(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Employee entity, long rowId) {
        entity.setUid(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Employee entity) {
        if(entity != null) {
            return entity.getUid();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "employeeList" to-many relationship of Department. */
    public List<Employee> _queryDepartment_EmployeeList(Long pid) {
        synchronized (this) {
            if (department_EmployeeListQuery == null) {
                QueryBuilder<Employee> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Pid.eq(null));
                department_EmployeeListQuery = queryBuilder.build();
            }
        }
        Query<Employee> query = department_EmployeeListQuery.forCurrentThread();
        query.setParameter(0, pid);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getDepartmentDao().getAllColumns());
            builder.append(" FROM EMPLOYEE T");
            builder.append(" LEFT JOIN DEPARTMENT T0 ON T.'PID'=T0.'PID'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Employee loadCurrentDeep(Cursor cursor, boolean lock) {
        Employee entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Department department = loadCurrentOther(daoSession.getDepartmentDao(), cursor, offset);
        entity.setDepartment(department);

        return entity;    
    }

    public Employee loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Employee> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Employee> list = new ArrayList<Employee>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Employee> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Employee> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
